import uploadImage from 'components/iz-aloss-uploader/iz-aloss-uploader.js';

export default {
	//file对象, 返回方法
	//url = 文件地址
	aliyun_upload(url, callback,complate,fix,pathFix) {
			let self = this;
			fix = fix ? fix : 'png'
			complate = complate ? complate : function(){}
			uploadImage(url, fix,
				result => {
					callback(result, '');
				}, err => {
					callback('', err);
				},res => {
					complate(res)
				},pathFix
			)
			
		},

	randomString(e) {
		e = e || 32;
		var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
			a = t.length,
			n = "";

		for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
		return n
	},
	creatFileName() {

		var myDate = new Date();

		return myDate.toLocaleDateString() + myDate.getHours() + myDate.getMinutes() + myDate.getSeconds();
	},

	queryUrlParams(name, url) {
		if (!url) {
			//  url = window.location.href;
			return '';
		}
		name = name.replace(/[\[\]]/g, "\\$&");
		var regex = new RegExp("[?&/]" + name + "([=/]([^&#/?]*)|&|#|$)"),
			results = regex.exec(url);
		if (!results)
			return null;
		if (!results[2])
			return '';
		return decodeURIComponent(results[2].replace(/\+/g, " "));
	},
	getBase64(url, callback) {

		let self = this;
		self.getBase64Image(url).then(ret => {
			callback(ret)
		}); // 将图片转成base64格式的方法
		// var img = new Image();
		// img.crossOrigin = ""; //跨域
		// //  document.body.appendChild(img)
		// //	img.src = $('#img').attr('src');

		// img.src = url;
		// img.onload = function() { // 图片加载后执行的方法
		// }

	},
	getBase64Image(img) {
		var canvas = document.createElement("canvas"); // 创建一个canvas
		canvas.width = img.width; // 设置对应的宽高
		canvas.height = img.height;
		var ctx = canvas.getContext("2d"); // 二维绘图环境
		ctx.drawImage(img, 0, 0, img.width, img.height); // 将图片画在画布上
		var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase(); // 获取到图片的格式
		var dataURL = canvas.toDataURL("image/" + ext); // 得到base64 编码的 dataURL
		// console.log(dataURL)
		return dataURL;

	},
	base64ToBlob: function(code) {

		let parts = code.split(';base64,');

		let contentType = parts[0].split(':')[1];
		let raw = window.atob(parts[1]);
		let rawLength = raw.length;
		let uInt8Array = new Uint8Array(rawLength);

		for (let i = 0; i < rawLength; ++i) {
			uInt8Array[i] = raw.charCodeAt(i);
		}
		return new Blob([uInt8Array], {
			type: contentType
		});
	},
	
	
	update_version(packageInfo) {
		console.log(packageInfo);
		/**
		 * 使用 uni.showModal 升级
		 */
		const {
			title, // 标题
			contents, // 升级内容
			is_mandatory, // 是否强制更新
			url, // 安装包下载地址
			platform, // 安装包平台
			type // 安装包类型
		} = packageInfo;

		let isWGT = type === 'wgt'
		let isiOS = !isWGT ? platform.includes('iOS') : false;
		let confirmText = isiOS ? '立即跳转更新' : '立即下载更新'

		return uni.showModal({
			title,
			content: contents,
			showCancel: !is_mandatory,
			confirmText,
			success: res => {
				if (res.cancel) return;

				// 安装包下载
				if (isiOS) {
					plus.runtime.openURL(url);
					return;
				}

				uni.showToast({
					title: '后台下载中……',
					duration: 1000
				});

				// wgt 和 安卓下载更新
				let downloadTask = uni.downloadFile({
					url,
					success: res => {
						if (res.statusCode !== 200) {
							console.error('下载安装包失败', err);
							return;
						}
						console.log(res.tempFilePath);
						// 下载好直接安装，下次启动生效
						plus.runtime.install(res.tempFilePath, {
							force: false
						}, () => {
							if (is_mandatory) {
								//更新完重启app
								plus.runtime.restart();
								return;
							}
							uni.showModal({
								title: '安装成功是否重启？',
								success: res => {
									if (res.confirm) {
										//更新完重启app
										plus.runtime.restart();
									}
								}
							});
						}, err => {
							uni.showModal({
								title: '更新失败',
								content: '',
								showCancel: false
							});
						});
					}
				});
			}
		});
	},

	/**
	 * 对比版本号，如需要，请自行修改判断规则
	 * 支持比对	("3.0.0.0.0.1.0.1", "3.0.0.0.0.1")	("3.0.0.1", "3.0")	("3.1.1", "3.1.1.1") 之类的
	 * @param {Object} v1
	 * @param {Object} v2
	 * v1 > v2 return 1
	 * v1 < v2 return -1
	 * v1 == v2 return 0
	 */
	compare(v1 = '0', v2 = '0') {
		v1 = String(v1).split('.')
		v2 = String(v2).split('.')
		const minVersionLens = Math.min(v1.length, v2.length);

		let result = 0;
		for (let i = 0; i < minVersionLens; i++) {
			const curV1 = Number(v1[i])
			const curV2 = Number(v2[i])

			if (curV1 > curV2) {
				result = 1
				break;
			} else if (curV1 < curV2) {
				result = -1
				break;
			}
		}

		if (result === 0 && (v1.length !== v2.length)) {
			const v1BiggerThenv2 = v1.length > v2.length;
			const maxLensVersion = v1BiggerThenv2 ? v1 : v2;
			for (let i = minVersionLens; i < maxLensVersion.length; i++) {
				const curVersion = Number(maxLensVersion[i])
				if (curVersion > 0) {
					v1BiggerThenv2 ? result = 1 : result = -1
					break;
				}
			}
		}

		return result;
	}
	
	



}
